<?php
/**
 * Created by PhpStorm.
 * User: yfl
 * Date: 2021/1/13
 * Time: 22:00
 */

namespace app\printer\controller;

//库存
use think\Db;
use think\facade\Request;

class InventoryController extends BaseController
{
    public function index()
    {
        return $this->fetch();
    }

    //得到数据展示到表格
    public function getData()
    {
        $page = Request::get('page', 0);
        $limit = Request::get('limit', 15);
        $data = Db::name('inventory')->page($page, $limit)->order(['id'=>'desc','create_time'=>'desc'])->select();
        $count = Db::name('inventory') ->count();
        foreach ($data as $index => $datum) $data[$index]['buy_num'] = 1;
        return ['code' => 0,'count'=> $count, 'data' => $data];
    }
    //添加
    public function add()
    {

        if(Request::isPost()){
            $post_data = Request::post();
            $data = [];
            foreach ($post_data as $key => $post_datum) {
                foreach ($post_datum as $index => $item) {
                    if(empty($post_data['num'][$index])) continue;
                    $data [$index]= [
                        'print_type' => $post_data['print_type'][$index],
                        'consumable_type' => $post_data['consumable_type'][$index],
                        'page_num' => $post_data['page_num'][$index],
                        'price' => $post_data['price'][$index],
                        'num' => $post_data['num'][$index],
                        'surplus_num' => $post_data['num'][$index],
                        'price_total' => $post_data['price_total'][$index],
                        'comment' => $post_data['comment'][$index],
                        'create_time' => getDateTime()
                    ];
                }
            }
            //todo 入库记录
            $res = Db::name('inventory')->insertAll($data);
            if($res){
                return $this->ajaxSuccess();
            }else{
                return $this->ajaxFail();
            }
        }
        echo  $this->fetch();
    }
    //编辑
    public function edit()
    {
        return $this->fetch();
    }

    // 出库记录
    //得到打印机库存记录
    public function inventoryRecode()
    {
        if(Request::isAjax()){
            $recode = new RecodeController();
            $data = $recode ->getInventoryRecode();
            return $data;
        }
        return $this->fetch();
    }

    //删除
    public function del()
    {
        $id  = Request::param('id');
        $res = Db::name('inventory')->delete($id);
        if($res){
            return $this->ajaxSuccess();
        }else{
            return $this->ajaxFail();
        }
    }
}